package com.psbc.redis_study.controller;

import com.psbc.redis_study.model.ProductDO;
import com.psbc.redis_study.service.ProductService;
import com.psbc.redis_study.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @PostMapping("/add")
    public JsonData add(@RequestBody ProductDO productDO){
        productDO.setCreateTime(new Date());
        int add = productService.add(productDO);
        return JsonData.buildSuccess(add);
    }
    @DeleteMapping("/deleteById")
    public JsonData delByIId(int id){
        int del = productService.delById(id);
        return JsonData.buildSuccess(del);
    }
    @PostMapping("/updateById")
    public JsonData updateById(@RequestBody ProductDO productDO){
        ProductDO result = productService.updateById(productDO);
        return JsonData.buildSuccess(result);
    }
    @GetMapping("/findById")
    public JsonData findById(int id){
        ProductDO productDO = productService.findById(id);
        return JsonData.buildSuccess(productDO);
    }
    @GetMapping("/find_page")
    public JsonData selectPage(int page,int size){
        Map<String, Object> map = productService.page(page, size);
        return JsonData.buildSuccess(map);
    }
}
